package com.ruoyi.manager.service.impl;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.manager.domain.SoldierBaseInfo;
import com.ruoyi.manager.domain.SoldierSpecialCareInfo;
import com.ruoyi.manager.mapper.SoldierBaseInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.manager.mapper.SoldierArrangeInfoMapper;
import com.ruoyi.manager.domain.SoldierArrangeInfo;
import com.ruoyi.manager.service.ISoldierArrangeInfoService;

/**
 * 退役军人_安置信息Service业务层处理
 *
 * @author Allen
 * @date 2021-08-30
 */
@Service
public class SoldierArrangeInfoServiceImpl extends ServiceImpl<SoldierArrangeInfoMapper, SoldierArrangeInfo> implements ISoldierArrangeInfoService {
    @Autowired
    private SoldierArrangeInfoMapper soldierArrangeInfoMapper;
    @Autowired
    private SoldierBaseInfoMapper soldierBaseInfoMapper;

    /**
     * 查询退役军人_安置信息
     *
     * @param id
     *         退役军人_安置信息主键
     *
     * @return 退役军人_安置信息
     */
    @Override
    public SoldierArrangeInfo selectSoldierArrangeInfoById(Long id) {
        return soldierArrangeInfoMapper.selectSoldierArrangeInfoById(id);
    }

    /**
     * 查询退役军人_安置信息列表
     *
     * @param soldierArrangeInfo
     *         退役军人_安置信息
     *
     * @return 退役军人_安置信息
     */
    @Override
    public List<SoldierArrangeInfo> selectSoldierArrangeInfoList(SoldierArrangeInfo soldierArrangeInfo) {
        List<SoldierArrangeInfo> arrangeInfoList = soldierArrangeInfoMapper.selectSoldierArrangeInfoList(soldierArrangeInfo);
        List<Long> soldierIdList = arrangeInfoList.stream().map(SoldierArrangeInfo::getSoldierId).collect(Collectors.toList());
        if (CollectionUtil.isNotEmpty(arrangeInfoList)) {
            Map<Long, SoldierBaseInfo> baseInfoMap = soldierBaseInfoMapper.selectList(new QueryWrapper<SoldierBaseInfo>().lambda().in(SoldierBaseInfo::getId, soldierIdList)).stream().collect(Collectors.toMap(SoldierBaseInfo::getId, y -> y));
            arrangeInfoList.forEach(x -> {
                x.setSoldierName(CollectionUtil.isEmpty(baseInfoMap) ? "" : baseInfoMap.get(x.getSoldierId()) == null ? "" : baseInfoMap.get(x.getSoldierId()).getName());
                x.setIdCard(CollectionUtil.isEmpty(baseInfoMap) ? "" : baseInfoMap.get(x.getSoldierId()) == null ? "" : baseInfoMap.get(x.getSoldierId()).getIdNumber());
            });
        }
        return arrangeInfoList;
    }

    /**
     * 新增退役军人_安置信息
     *
     * @param soldierArrangeInfo
     *         退役军人_安置信息
     *
     * @return 结果
     */
    @Override
    public int insertSoldierArrangeInfo(SoldierArrangeInfo soldierArrangeInfo) {
        return soldierArrangeInfoMapper.insertSoldierArrangeInfo(soldierArrangeInfo);
    }

    /**
     * 修改退役军人_安置信息
     *
     * @param soldierArrangeInfo
     *         退役军人_安置信息
     *
     * @return 结果
     */
    @Override
    public int updateSoldierArrangeInfo(SoldierArrangeInfo soldierArrangeInfo) {
        return soldierArrangeInfoMapper.updateSoldierArrangeInfo(soldierArrangeInfo);
    }

    /**
     * 批量删除退役军人_安置信息
     *
     * @param ids
     *         需要删除的退役军人_安置信息主键
     *
     * @return 结果
     */
    @Override
    public int deleteSoldierArrangeInfoByIds(Long[] ids) {
        return soldierArrangeInfoMapper.deleteSoldierArrangeInfoByIds(ids);
    }

    /**
     * 删除退役军人_安置信息信息
     *
     * @param id
     *         退役军人_安置信息主键
     *
     * @return 结果
     */
    @Override
    public int deleteSoldierArrangeInfoById(Long id) {
        return soldierArrangeInfoMapper.deleteSoldierArrangeInfoById(id);
    }
}
